package com.softWang.file.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.softWang.file.constants.FileSavePath;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

import com.softWang.common.response.PageBean;
import com.softWang.common.request.Idparam;
import com.softWang.common.response.ResponseResult;
import com.softWang.file.po.FileManage;
import com.softWang.file.service.FileManageService;
import com.softWang.file.vo.fileManage.*;
import org.springframework.web.multipart.MultipartFile;

@Api(value="文件上传下载API",tags="文件上传下载API")
@Validated
@RestController
@RequestMapping("/file/fileManage")
public class FileManageController {
     @Resource
     private FileManageService fileManageService;

     @ApiOperation(value="列表(分页)")
     @RequestMapping(value = "/query", method = RequestMethod.POST)
     public ResponseResult<PageBean<FileManageQueryAllResult>> queryall(@RequestBody @Validated FileManageQueryAllParam param, HttpServletRequest request) {
         List<FileManageQueryAllResult> results=fileManageService.queryall(param);
         PageBean page=new PageBean(results);
         return ResponseResult.success(page);
     }

    /**
     * 通用上传请求（单个）
     */
    @ApiOperationSupport(ignoreParameters = {})
    @ApiOperation(value = "上传文件",notes = "返回数据格式{fileId:\"id\",oriName:\"name\"}")
    @PostMapping("/upload")
    public ResponseResult uploadFile(MultipartFile file) throws Exception
    {
        Map map = fileManageService.uploadFile(file, FileSavePath.UPLOAD.getSubPath());
        return ResponseResult.success(map);
    }


    /**
     * 通用上传请求（多个）
     */
    @ApiOperationSupport(ignoreParameters = {})
    @ApiOperation(value = "上传多文件",notes = "返回数据格式List<Map>,map格式{fileId:\"id\",oriName:\"name\"}")
    @PostMapping("/uploads")
    public ResponseResult uploadFiles(List<MultipartFile> files) throws Exception{
        List<Map> map = fileManageService.uploadFiles(files,FileSavePath.UPLOAD.getSubPath());
        return ResponseResult.success(map);
    }


    @ApiOperationSupport(ignoreParameters = {})
    @ApiOperation(value = "获取文件下载url",notes = "返回格式{fileUrl:\"url\"}")
    @PostMapping("/getFileUrl")
    public ResponseResult getFileUrl(@RequestBody @Validated GetFileUrlParam param)throws Exception{
        Map map = fileManageService.getFileUrl(param.getFileId());
        return ResponseResult.success(map);
    }
    @ApiOperation(value = "删除文件")
    @PostMapping("/del")
    public ResponseResult delFiles(@RequestBody @NotEmpty List<@NotNull Long> fileIds)throws Exception{
        for(Long fileId: fileIds){
            fileManageService.deleteFile(fileId);
        }
        return ResponseResult.success();
    }

}